<?
require "config.php";
require "dojob.php";

function err($msg)
{
?>
    <html>
    <body>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf8">
    <h1><?echo $msg;?></h1>
    </head>
    </body>
    </html>
<?
}

session_start();
$email = $_SESSION['email'];
if (empty($email)) {
    header("Location: index.php");
    session_destroy();
    return;
}
if (isset($_GET['run'])) {
    $jarfile = trim($_POST['jobs']);
    $args = trim($_POST['exist_args']);
    $jarfile = explode("|", $jarfile);
    $jarfile = $jarfile[0];
    if (empty($jarfile) | empty($args)) {
        err("Please specify jar file or argument");
        return;
    }
    dojob($jarfile, $args, $email);
?>
            <html>
            <body>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf8">
            <h1>Job is commited, Please note email</h1>
            </head>
            </body>
            </html>
<?
} else if (isset($_GET['commit'])) {
    global $HADOOP_JOBS_DIR;
    $jardir = $HADOOP_JOBS_DIR;

    if ($_FILES['file']['error'] > 0) {
?>
        <html>
        <body>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf8">
        <h1>Upload file error: <?echo "Jar file can't be empty";?></h1>
        </head>
        </body>
        </html>
<?
        return;
    }
    $filename = $_FILES['file']['name'];
    $filesize = $_FILES['file']['size'];
    $filetmp = $_FILES['file']['tmp_name'];
    $filetype = $_FILES['file']['type'];
    $args = $_POST['args'];

    if (!empty($filename) && $filesize > 0) {
        $jarfile = $jardir."/".$filename;
        if (move_uploaded_file($filetmp, $jarfile)) {
            $db = mysql_connect("localhost", "hdfs", "hdfs");
            if (!$db) {
                err("Colud not connect: " . mysql_error());
                session_destroy();
                return;
            }
            if (!mysql_select_db("hadoop", $db)) {
                err("Colude not select db hadoop: " . mysql_error());
                mysql_close($db);
                session_destroy();
                return;
            }
            $query = sprintf("select count(*) from jobs where jar='%s';",
                    mysql_real_escape_string($jarfile));
            $result = mysql_query($query, $db);
            if (!$result) {
                err("select jar error: " . mysql_error());
                mysql_close($db);
                session_destroy();
                return;
            }
            $count = mysql_fetch_array($result);

            if ($count[0] > 0) {
                $query = sprintf("update jobs set jar='%s', args='%s' where jar='%s';",
                        mysql_real_escape_string($jarfile),
                        mysql_real_escape_string($args),
                        mysql_real_escape_string($jarfile));
                $a = "update";
            } else {
                $query = sprintf("insert into jobs (jar, args) values ('%s', '%s');",
                        mysql_real_escape_string($jarfile),
                        mysql_real_escape_string($args));
                $a = "insert";
            }
            $result = mysql_query($query, $db);
            if (!$result) {
                mysql_close($db);
                session_destroy();
                return;
            }
            dojob($jarfile, $args, $email);
?>
            <html>
            <body>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf8">
            <h1>Upload file successed (<?echo $a;?>), Job is running, Please note email</h1>
            </head>
            </body>
            </html>
<?
        } else {
?>
            <html>
            <body>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf8">
            <h1>Upload file failed</h1>
            </head>
            </body>
            </html>
<?
        }
    }
}
session_destroy();
?>
